<?php
namespace app\admin\controller;
use app\common\controller\Admin;
use app\common\model\Page as PageModel;
class Page extends Admin{
   /**
    * 单页管理首页
    */
    public function index(){
        $where=[];
        $order='id desc';
        $this->assign('list', PageModel::getPageList($where,$order));
        $this->setMeta('文章管理');
        return $this->fetch('index');         
    }
    /**
     * 单页编辑
     */
    public function edit($id){
        $page = model('Page');
        if ($this->request->isPost()) {
            $result =$page->save($this->request->param(), ['id' => $id]);
            if ($result !== false) {
                return $this->success("编辑成功", url('index'));
            } else {
                return $this->error($page->getError());
            }
        } else {
            $info = db('page')->where(array('id' => $id))->find();
            if (!$info) {
                return $this->error("非法操作！");
            }
            $data = array(
                'info' => $info,
                'keyList' => $page->keyList,
            );
            $this->assign($data);
            $this->setMeta("编辑分类");
            return $this->fetch('public/edit');
        }
    }
    /**
     * 单页添加
     */
    public function add(){
        $page = model('Page');
        if ($this->request->isPost()) {
            $result =$page->save($this->request->param());
            if ($result !== false) {
                return $this->success("添加成功", url('index'));
            } else {
                return $this->error($page->getError());
            }
        } else {
            $data = array(
                'keyList' => $page->keyList,
            );
            $this->assign($data);
            $this->setMeta("添加分类");
            return $this->fetch('public/edit');
        }
    }
    /**
     * 单页删除
     */
    public function delete(){
        $page = model('Page');
        $param = $this->request->param();
        $id = $param['id'];
        if (empty($id)) {
            return $this->error("非法操作！");
        }

        $map['id'] = array('IN', $id);
        $result =$page->where($map)->delete();

        if (false !== $result) {
            //记录行为
            action_log('delete_page', 'page', $result, session('auth_user.uid'));
            return $this->success("删除成功！");
        } else {
            return $this->error("删除失败！");
        }
    }
}

